//小仓库 关于layout的设置
import { defineStore } from "pinia";
import { reqSearchAdmin, reqAddAdmin, reqUpdateAdmin, reqDeleteAdminById, reqDeleteAdminByIds } from "@/api/admin"
import { ElMessage } from "element-plus";

let useAdminStore = defineStore("AdminStore", {
    state: () => {
        return {
            admin: [],
            total: 0,
        };
    },
    // 异步 | 逻辑的地方
    actions: {
        async searchAdmin(data: any) {
            let res = await reqSearchAdmin(data);
            if (res.code == 200) {
                if (res.data.list.length == 0) {
                    return false;
                } else {
                    this.admin = res.data.list;
                    this.total = res.data.total;
                    return true;
                }
            }
        },
        async addAdmin(data: any) {
            var data1 = JSON.stringify(data);
            let res = await reqAddAdmin(data1);
            if (res.code == 200) {
                ElMessage({
                    message: "添加成功",
                    type: 'success',
                })
            } else {
                ElMessage({
                    message: "添加失败",
                    type: 'error',
                })
            }
        },
        async updateAdmin(data: any) {
            var data1 = JSON.stringify(data);
            let res = await reqUpdateAdmin(data1);
            if (res.code == 200) {
                ElMessage({
                    message: "修改成功",
                    type: 'success',
                })
            }
            else {
                ElMessage({
                    message: "修改失败",
                    type: 'error',
                })
            }
        },
        async deleteAdminById(id: any) {
            let res = await reqDeleteAdminById(id);
            if (res.code == 200) {
                ElMessage({
                    message: "删除成功",
                    type: 'success',
                })
                return true;
            }
            else {
                ElMessage({
                    message: "删除失败",
                    type: 'error',
                })
                return false;
            }
        },
        async deleteAdminByIds(ids: any) {
            let res = await reqDeleteAdminByIds(ids);
            if (res.code == 200) {
                ElMessage({
                    message: "批量删除成功",
                    type: 'success',
                })
                return true;
            }
            else {
                ElMessage({
                    message: "批量删除失败",
                    type: 'error',
                })
                return false;
            }
        }
    },
    getters: {

    }
});

export default useAdminStore;